package chainofresponsibility.main.process;

import chainofresponsibility.main.handle.IHandler;
import chainofresponsibility.main.impl.SchoolMasterHandler;
import chainofresponsibility.main.impl.SquadLeaderHandler;
import chainofresponsibility.main.impl.TeacherHandler;
import chainofresponsibility.main.message.IStudent;

/**
 * 外观类，处理学生请假请求
 */
public class ProcessHandler {

    //班长处理者
    private final IHandler squadLeaderHandler;
    //老师处理者
    private final IHandler teachHandler;
    //校长处理者
    private final IHandler schoolMasterHandler;

    //单例模式
    private static ProcessHandler processHandler = new ProcessHandler();

    /**
     * 构造方法，建立请假责任链
     */
    private ProcessHandler() {
        //创建处理对象
        //班长
        this.squadLeaderHandler = new SquadLeaderHandler();
        //老师
        this.teachHandler = new TeacherHandler();
        //校长
        this.schoolMasterHandler = new SchoolMasterHandler();
        /**
         * 建立责任链
         */
        //设置班长的下一个处理者:老师
        this.squadLeaderHandler.setHandler(this.teachHandler);
        //设置老师的下一个处理者：校长
        this.teachHandler.setHandler(this.schoolMasterHandler);
    }

    /**
     * 获得单例对象实例
     */
    public static ProcessHandler getInstance() {
        return processHandler;
    }

    /**
     * 发送请假请求
     */
    public void sendMessage(IStudent student) {
        //发送给第一个处理者：班长处理
        this.squadLeaderHandler.handleRequest(student);
    }
}
